﻿/**
 * Created by Administrator on 2017/8/18 0018.
 */
var express = require('express');
var crypto = require("crypto");
var qr = require('qr-image')
var multer = require("multer");
var config = require('../../config.js');
var db = require('../../db/db.js');
var moment = require("moment");

var router = express.Router();
var applet_model = require('../../model/applet.js');



router.post('/add', async function (req, res) {

    var data = req.body;
    data.createtime = new Date();
    var obj = await applet_model.add(data);
    if (obj.code == 200) {
        var result = {
            "status": "200",
            "message": "success"
        }
        res.jsonp(result);
    } else {
        var result = {
            "status": "400",
            "message": "error"
        }
        res.jsonp(result);
    }


})


router.post("/edit", async function (req, res) {

    var data = req.body;
    data.createtime = new Date();
    var obj = await applet_model.edit(data);
    if (obj.code == 200) {
        var result = {
            "status": "200",
            "message": "success"
        }
        res.jsonp(result);
    } else {
        var result = {
            "status": "400",
            "message": "error"
        }
        res.jsonp(result);
    }

})

router.post('/list', async function (req, res) {
    var page;
    var rows;
    if (isNaN(req.body.page)) {
        page = 1;
    } else {
        page = +req.body.page;
    }
    if (isNaN(req.body.rows)) {
        rows = 10;
    } else {
        rows = +req.body.rows;
    }
    var first = rows * (page - 1);

    var where = " 1=1 ";
    if (typeof (req.body.name) != "undefined" && req.body.name != "") {
        where = where + " and name like '%" + req.body.name + "%'";

    }






    console.log("where", where);
    var list = await applet_model.getlist(rows, first, where);
    for (var i = 0; i < list.length; i++) {


    }
    var count = await applet_model.getcount(where);
    var allcount = parseInt(parseInt(count) / rows) + 1;
    if (count % rows == 0) {
        allcount = allcount - 1;
    }

    res.send({ status: 200, data: list, message: "", allcount: allcount, count: count });

});




router.delete('/del', async function (req, res) {

    var where = " id in (" + req.body.ids + ")";
    var obj = await applet_model.del(where);
    if (obj.code == 200) {
        var result = {
            "status": "200",
            "message": "success"
        }
        res.jsonp(result);
    } else {
        var result = {
            "status": "400",
            "message": "success"
        }
        res.jsonp(result);
    }


});




module.exports = router;   /*暴露这个 router模块*/